import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/page/home/model/common_tag_model.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';

/// 选中Item的回调
///  itemIndex item索引
///  cityModel 选中的数据
typedef SelectDropMenuListItemCallback(int itemIndex, CommonTagModel tagModel);

class DropDownListWidget extends StatefulWidget {
  /// 标签数据
  final List<CommonTagModel> listDatas;

  /// 选择之后的回调
  final SelectDropMenuListItemCallback selectListItemCallback;

  const DropDownListWidget(
      {Key key, this.listDatas, this.selectListItemCallback})
      : super(key: key);

  @override
  _DropDownListWidgetState createState() => _DropDownListWidgetState();
}

// ignore: must_be_immutable
class _DropDownListWidgetState extends State<DropDownListWidget> {
  /// 行高
  final double cellHeight = ScreenAdapter.height(90);

  @override
  Widget build(BuildContext context) {
    return _buildAddressWidget();
  }

  /// 其他条件列表
  _buildAddressWidget() {
    return ListView.separated(
      shrinkWrap: true,
      scrollDirection: Axis.vertical,
      itemCount: widget.listDatas.length,
      // item 的个数
      separatorBuilder: (BuildContext context, int index) =>
          Divider(height: 0.5, color: ThemeColors.colorType49),
      // 添加分割线
      itemBuilder: (BuildContext context, int index) {
        CommonTagModel tagModel = widget.listDatas[index];
        return InkWell(
          onTap: () {
            for (var value in widget.listDatas) {
              value.isSelected = false;
            }
            tagModel.isSelected = true;

            widget.selectListItemCallback(index, tagModel);
          },
          child: Container(
              height: cellHeight,
              padding: EdgeInsets.only(
                  left: ScreenAdapter.width(80),
                  right: ScreenAdapter.width(80)),
              child: IntrinsicHeight(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text(
                      tagModel.title,
                      style: TextStyle(
                          color: tagModel.isSelected
                              ? ThemeColors.colorType6
                              : ThemeColors.colorType1,
                          fontSize: ScreenAdapter.sp(30)),
                    ),
                    tagModel.isSelected
                        ? Icon(
                            Icons.check,
                            color: ThemeColors.colorType6,
                            size: 16,
                          )
                        : SizedBox(),
                  ],
                ),
              )),
        );
      },
    );
  }
}
